package com.efast.cafe.portal.controller.api;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.efast.cafe.framework.bean.ResultBean;
import com.efast.cafe.framework.controller.AbstractController;
import com.efast.cafe.framework.exception.ServiceException;
import com.efast.cafe.framework.vo.DataTableVo;
import com.efast.cafe.portal.bean.api.ApiReqUriSampleQueryBean;
import com.efast.cafe.portal.entity.api.ApiReqUriSample;
import com.efast.cafe.portal.service.api.IApiReqUriSampleService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.support.RequestContext;
@Controller
@RequestMapping("/frame_center/api")
public class ApiReqUriSampleController extends AbstractController{

	@Autowired
	private IApiReqUriSampleService apiReqUriSampleService;
	
	/**
	 * 查询uri 数据示例列表
	 * 
	 * @param queryBean
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/urisample/list")
	public @ResponseBody DataTableVo<ApiReqUriSample> templatePage(@ModelAttribute ApiReqUriSampleQueryBean queryBean,
			HttpServletRequest request, HttpServletResponse response) {

		return outPageJSONArray_bootstrapDatatable(apiReqUriSampleService.queryPageList(queryBean));

	}

	/**
	 * 添加uri数据示例
	 * @param apiReqUriSample
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/urisample/add")
	public @ResponseBody ResultBean addUriSample(@ModelAttribute ApiReqUriSample apiReqUriSample, HttpServletRequest request,
			HttpServletResponse response) {
		RequestContext context = new RequestContext(request);
		ResultBean result = new ResultBean(true, context.getMessage(ADD_SUCCESS_TIP));
		Integer id = null;
		try {

			id = apiReqUriSampleService.addUriSample(apiReqUriSample);
			result.setObject(id.toString());
		} catch (Exception e) {
			result.setSuccess(false);
			if (e instanceof ServiceException) {
				result.setMessage(context.getMessage(e.getMessage()));
			} else {
				result.setMessage(context.getMessage(ADD_FAILURE_TIP));
			}
		}

		return result;
	}

	/**
	 *  编辑uri数据示例
	 * @param apiReqUriSample
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/urisample/edit")
	public @ResponseBody ResultBean editUriSample(@ModelAttribute ApiReqUriSample apiReqUriSample, HttpServletRequest request,
			HttpServletResponse response) {
		RequestContext context = new RequestContext(request);
		ResultBean result = new ResultBean(true, context.getMessage(EDIT_SUCCESS_TIP));
		try {

			apiReqUriSampleService.editUriSample(apiReqUriSample);
		} catch (Exception e) {
			result.setSuccess(false);
			if (e instanceof ServiceException) {
				result.setMessage(context.getMessage(e.getMessage()));
			} else {
				result.setMessage(context.getMessage(EDIT_FAILURE_TIP));
			}
		}

		return result;
	}

	/**
	 * 删除 uri数据示例
	 * @param id
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/urisample/del")
	public @ResponseBody ResultBean delUriSample(@RequestParam(required = false) Integer id, HttpServletRequest request,
			HttpServletResponse response) {
		RequestContext context = new RequestContext(request);
		ResultBean result = new ResultBean(true, context.getMessage(DEL_SUCCESS_TIP));
		try {
			apiReqUriSampleService.delUriSample(id);
		} catch (Exception e) {
			result.setSuccess(false);
			if (e instanceof ServiceException) {
				result.setMessage(context.getMessage(e.getMessage()));
			} else {
				result.setMessage(context.getMessage(DEL_FAILURE_TIP));
			}
		}
		return result;
	}

	
}
